package oop.ex2.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;

import oop.ex2.enums.CommentEnum;
import oop.ex2.enums.MethodEnum;
import oop.ex2.enums.VariableEnum;
import oop.ex2.objects.Method;
import oop.ex2.objects.Variable;
import oop.ex2.compiler.*;
import oop.ex2.compiler.Compiler;

public class Sjavac {

    static boolean isInsideComment = false;
    static ArrayList<String> fileContents = new ArrayList<String>();

    /**
     * @param args
     */
    public static void main(String[] args) {
        File file = new File(args[0]);
        ArrayList<Variable> globalVariables = new ArrayList<Variable>();
        HashMap<String, Method> methodsDeclared = new HashMap<String, Method>();
        String currentLine;
        BufferedReader bufferedReader = null;
        Reader fileReader = null;

        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
            currentLine = bufferedReader.readLine();
            while (currentLine != null) { // Get contents of file to arrayList.
                if (!currentLine.isEmpty()) {
                    fileContents.add(currentLine);
                }
                currentLine = bufferedReader.readLine();
            }
            Compiler compiler = new Compiler(fileContents, globalVariables, methodsDeclared);
            compiler.checkCode();
        } catch (IOException e) {
            throwIOException(e);

        } catch (IllegalCodeException e) {
            // System.err.println("Error: " + e);
            e.printStackTrace();
            System.exit(1);
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        for (String s : fileContents) {
            System.out.println(s);
        }
        // System.out.println("The code is a valid S-Java code."); // If no
        // exception was
        // thrown
        System.exit(0);
    }

    private static void throwIOException(IOException e) {
        // System.err.println("Error: " + e);
        e.printStackTrace();
        System.exit(2);
    }

    
}
